package com.efivestar.idm.api.service.lock.protocol.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efivestar.idm.api.common.Cons;
import com.efivestar.idm.api.domain.EventDomain;
import com.efivestar.idm.api.domain.LockRemoteControlDomain;
import com.efivestar.idm.api.repository.EventMapper;
import com.efivestar.idm.api.service.WeixinService;
import com.efivestar.idm.api.util.ByteUtils;

import io.netty.buffer.ByteBuf;

/**
 * 查询锁的是否允许被远程打开状态.
 */
@Service
public class RequestDoorStatusProtocolHandler extends AbstractLockProtocolHandler{

    @Autowired
    private EventMapper mapper;

    @Autowired
    private WeixinService weixinService;

    /**
     * @param buf
     * @param lockId
     * @return
     */
    @Override
    public byte[] handleData(ByteBuf buf, String lockId) {
        int length = buf.readableBytes();
        //跳过指令码，不读取
        buf.skipBytes(1);
        //读取imei(设备编号)
        // String lockId = String.valueOf(buf.readCharSequence(length - getDataLenthWithoutDeviceId(), Charset.forName("utf-8")));
        //设置请求远程开门的状态.
        LockRemoteControlDomain lockRemoteControl = weixinService.getLockRemoteControl(lockId);
//        让王彬能看到数据
        EventDomain event = EventDomain.builder().deviceId(lockId).eventType(100).subEventType(100).content("").createTime(new Date()).build();
        mapper.insert(event);

        //用户已经操作
        if (lockRemoteControl.isOperated()){
            Date now = new Date();
            Date requestTime = lockRemoteControl.getRequestTime();
            //04代表，远程开锁请求已超时(6分钟，本来是5分钟向后延迟一分钟，因为用户和锁都要反应一段时间.)
            if (now.getTime() - requestTime.getTime() > 360000) {
                return  ByteUtils.hexToBytes( Cons.UDP_COMMAND_REMOTE_DOOR_CONOTROL_STATUS + "04" + numToHex32(lockRemoteControl.getSequence())) ;
            } else {
                //02代表，允许远程开锁
                if (lockRemoteControl.isAllowed()) {
                    return  ByteUtils.hexToBytes( Cons.UDP_COMMAND_REMOTE_DOOR_CONOTROL_STATUS + "02" + numToHex32(lockRemoteControl.getSequence())) ;
                } else {
                    //03代表，拒绝远程开锁
                    return  ByteUtils.hexToBytes( Cons.UDP_COMMAND_REMOTE_DOOR_CONOTROL_STATUS + "03" + numToHex32(lockRemoteControl.getSequence())) ;
                }
            }
        } else {
            //01代表用户尚未操作
            return  ByteUtils.hexToBytes( Cons.UDP_COMMAND_REMOTE_DOOR_CONOTROL_STATUS + "01" + numToHex32(lockRemoteControl.getSequence())) ;
        }
    }


    //int的16进制标识
    private String numToHex32(int b) {
        return String.format("%08x", b);
    }


    @Override
    protected int getDataLenthWithoutDeviceId() {
        return 1;
    }


    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }

    @Override
    public String udpHead(){
        return Cons.UDP_HEAD_EF01;
    }

    @Override
    public String udpCommand(){
        return Cons.UDP_COMMAND_REMOTE_DOOR_CONOTROL_STATUS;
    }


}






